var importHead = [];
var title = [
	{ "id": "order_num", "view": "订单编号" },
	{ "id": "goods", "view": "货品名称" },
	{ "id": "goods_sku", "view": "规格名称" },
	{ "id": "sum_goods", "view": "货品数量" },
	{ "id": "mem_name", "view": "收货人" },
	{ "id": "mem_phone", "view": "手机号" },
	{ "id": "detailaddress", "view": "收货地址" },
	// { "id": "offers", "view": "优惠" },
	// { "id": "postage", "view": "邮费" },
	{ "id": "remark", "view": "客服备注" }
	// { "id": "remark1", "view": "客户备注" }
];

layui.use(['form'], function () {
	var form = layui.form,
		layer = layui.layer,
		$ = layui.$;

		//初始化规则ID
	form.val("form1", {
		"rule_uid":uuid(),
	});

	//监听提交
	form.on('submit(saveBtn)', function () {
		let form1 = form.val('form1');
		let form2 = form.val('form2');
		// console.log("Debug form1:",form1);
		// console.log("Debug form2:",form2);
		// console.log("Debug arrdata:",arrdata(form1,form2));
		var index = layer.confirm("确认添加", function () {
			$.ajax({
				url:'/MappingRules/add',
				type:"post",
				data:JSON.stringify(arrdata(form1,form2)),
				dataType:"json",
				contentType:'application/json',  // 声明发送数据格式
				// processData:false,   // 浏览器不要影响数据
			});

			// 关闭弹出层
			layer.close(index);
			// 关闭弹出iframe
			var iframeIndex = parent.layer.getFrameIndex(window.name);
			parent.layer.close(iframeIndex);
		});

		return false;
	});

	window.initmap = function () { 
		$('#mapping tr').remove();
		console.log("Debug importHead:",importHead);

		var mappingRules = document.getElementById("mapping");
		// mappingRules.innerHTML = "<tr><th>序号</th><th>导入模板字段</th><th>导出模板字段</th></tr>";

		for (var x=0 ; x< title.length ; x++){

			var tr = mappingRules.insertRow(x);
		
			var xuhao = x+1;
			var exporthead = title[x].view;

			// tr.innerHTML="<td>"+ xuhao +'</td><td name="export'+ x +'" value= "'+ exporthead +'" class="layui-form-item">'+exporthead+"</td>";
			tr.innerHTML = "<td>" + xuhao + '</td><td>' + exporthead + "</td>";
		
			var select = document.createElement("select");
			select.setAttribute("name", exporthead);
			// select.setAttribute("class","importHead");
			var optEle = document.createElement("option");
			optEle.setAttribute("value", "default");
			optEle.appendChild(document.createTextNode("===未导入==="));
			select.appendChild(optEle);
			for (var y=-0 ; y<importHead.length;y++){
				var optEle1 = document.createElement("option");
				optEle1.setAttribute("value",importHead[y]);
				optEle1.appendChild(document.createTextNode(importHead[y]));
				select.appendChild(optEle1);
			}
			
			var td = document.createElement("td");
			td.appendChild(select);
			tr.appendChild(td);
		
			mappingRules.appendChild(tr);
		
		};
		
		layui.form.render("");
	}

	window.init = function () { 
		// 初始化生成两个select标签
		var objimport = document.getElementById("temp_name");
		// var objexport = document.getElementById("export");
		
		$.ajax({
			url:'/OrderTemp/listforselect',
			type:"get",
			datatype:"json",
			success:function(data){
				console.log("Debug data：",data);
				let optEle1 = document.createElement("option");
				optEle1.setAttribute("value","default");

				objimport.appendChild(optEle1);
				
				// let optEle2 = document.createElement("option");
				// optEle2.setAttribute("value","default");
				// optEle2.appendChild(document.createTextNode("===选择导出Excel模板==="));
				// objexport.appendChild(optEle2);
				
				for (var x=0 ; x<data.data.length;x++){
					
					let optEle1 = document.createElement("option");
					optEle1.setAttribute("value",data.data[x].temp_uid);
					optEle1.appendChild(document.createTextNode(data.data[x].temp_name));
					objimport.appendChild(optEle1);
					
					// let optEle2 = document.createElement("option");
					// optEle2.setAttribute("value",data.data[x].excel_id);
					// optEle2.appendChild(document.createTextNode(data.data[x].excel_name));
					// objexport.appendChild(optEle2);
				}
				layui.form.render("select");
			}
		});

		var dealerSelect = document.getElementById("dealer_id");
		$.ajax({
			url: '/Dealer/list',
			type: "post",
			data: `{"page":1,"limit":15}`,
			datatype: "json",
			success: function (data) {
				console.log("Debug data:", data);
				let optEle1 = document.createElement("option");
				// optEle1.setAttribute("value","default");
				dealerSelect.appendChild(optEle1);
				for (var x = 0; x < data.data.length; x++) {
					let optEle1 = document.createElement("option");
					optEle1.setAttribute("value", data.data[x].dealer_uid);
					optEle1.appendChild(document.createTextNode(data.data[x].dealer));
					dealerSelect.appendChild(optEle1);
				}
				layui.form.render("select");
			}
		});

		// 两个select标签，绑定change事件
		// 获取导入表头数据，及标签所用数据，生成数组保存
		layui.form.on('select(temp_name)', function(data){
		// $('#import').change(function(){
			// var importid = $("#import option:selected").val();
			// console.log("Debug data:",data);
			$.ajax({
				url:"/OrderTemp/view",
				async:"true",
				type:"get",
				datatype:"json",
				data:"id="+ data.value,
				success:function(res){
					console.log("Debug temp_name view res:",res);
					importHead = res;
					initmap();
				}
			});
			
		});
	}

});

function uuid(){   // 生成唯一字符串
	return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g,function (c) {
		var r =Math.random() * 16 | 0 ,
		 v = c == 'x' ? r: (r & 0x3|0x8);
		return v.toString(16);
	})
}

function arrdata(form1,form2) {
    let data = [];
    for (key in form2) {
        let obj = {};
        obj.rule_uid = form1.rule_uid;
        obj.exp_head = key;
		obj.imp_head = form2[key];
		obj.rule_name = form1.rule_name;
		obj.dealer_id = form1.dealer_id
		obj.remark = form1.remark;
        data.push(obj);
    }
    return data
}
